package com.lzs.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.entity.Menu;
import com.lzs.system.mapper.MenuMapper;
import com.lzs.system.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.system.vo.MenuTree;
import com.lzs.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-24
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private MenuMapper menuMapper;


    /**
     * 菜单条件查询
     * @param menu
     * @return
     */
    @Override
    public R selectAllMenu(Menu menu) {
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        String name = menu.getName();
        Integer levels = menu.getLevels();
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (levels!=null){
            queryWrapper.eq("levels",levels);
        }
        List<Menu> menus = menuMapper.selectList(queryWrapper);
        return R.ok().data("menus",menus);
    }



    /**
     * 菜单新增修改方法
     * @param menu
     * @return
     */
    @Override
    public R insertOrUpdateMenu(Menu menu) {
        if (menu.getMenuId()!=null){
            menu.setUpdateTime(new Date());
            int i = menuMapper.updateById(menu);
            if (i>0){
                return R.ok().message("修改成功");
            }
            return R.ok().message("修改失败");
        }else {
            String pcode = menu.getPcode();
            if(!StringUtils.isNotEmpty(pcode)){
                menu.setPcode("0");
            }
            menu.setCreateTime(new Date());
            int i = menuMapper.insert(menu);
            if (i>0){
                return R.ok().message("新增成功");
            }
            return R.ok().message("新增失败");
        }
    }

    /**
     * 修改回显方法
     * @param menuId
     * @return
     */
    @Override
    public R findMenuById(Long menuId) {
        Menu menu = menuMapper.findMenuById(menuId);
        return R.ok().data("menu",menu);
    }

    /**
     * 根据路径查找当前菜单名称
     * @param url
     * @return
     */
    @Override
    public Menu findByUrl(String url) {
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("url",url);
        Menu menu = menuMapper.selectOne(queryWrapper);
        return menu;
    }

    /**
     * 左侧菜单功能
     * @param userId
     * @return
     */
    //  左侧权限菜单
    @Override
    public List<MenuTree> selectMenuByUserId(Long userId) {
        List<MenuTree> firstMenu=new ArrayList<>();
        List<Menu> list = menuMapper.selectMenuByUserId(userId);
        for (Menu first:list){
            if (first.getPcode().equals("0")){
                MenuTree menuTree=new MenuTree();
                BeanUtils.copyProperties(first,menuTree);
                Boolean flag=true;
                for (MenuTree menuTree1:firstMenu){
                    if (menuTree1.getCode().equals(first.getCode())){
                        flag=false;
                    }
                }
                if(flag){
                    firstMenu.add(menuTree);
                }

            }
        }
        for (Menu second:list){
            for (MenuTree first:firstMenu){
                if (second.getPcode().equals(first.getCode())){
                    MenuTree menuTree=new MenuTree();
                    BeanUtils.copyProperties(second,menuTree);
                    Boolean flag=true;
                    for (MenuTree menuTree1:first.getChildren()){
                        if (menuTree1.getCode().equals(second.getCode())){
                            flag=false;
                        }
                    }
                    if(flag){
                        first.getChildren().add(menuTree);
                    }

                }
            }
        }
        return firstMenu;
    }

    // 查询所有权限
    @Override
    public List<Menu> selectMenuTreeByRoleId(Menu menus,Long roleId) {
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        String name = menus.getName();
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
            List<Menu> list = menuMapper.selectList(queryWrapper);
            return list;
        }else {
            List<Menu> list = menuMapper.selectList(null);
            List<Menu> root = new ArrayList<>();
            for (Menu menu : list) {
                if (menu.getPcode().equals("0")) {
                    root.add(menu);
                }
            }
            for (Menu m : root) {
                m.setChildren(selectChildMenu(m.getCode(), list));
            }
            return root;
        }
    }
    private List<Menu> selectChildMenu(String code, List<Menu> list) {
            List<Menu> children=new ArrayList<>();
            for(Menu menu:list){
                if(menu.getPcode().equals(code)){
                    children.add(menu);
                }
            }
            //遍历所有的children
            for(Menu c:children){
                c.setChildren(selectChildMenu(c.getCode(),list));
            }

        return children;
    }



}
